package rent_car.Utils;

import rent_car.domain.CarBase;
import org.dom4j.*;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.dom4j.tree.DefaultDocument;

import java.io.*;

public class XmlUtils {
    //文档的创建和xml文件的声明只需要一次就够了。
    static {
        XMLWriter writer = null;
        File file = new File("orders.xml");
        try {
            if(!file.exists()){
                writer = new XMLWriter(new FileOutputStream(file), OutputFormat.createPrettyPrint());
                Document doc = new DefaultDocument();  //创建一个默认的文档
                Element element = DocumentHelper.createElement("orders");  //创建一个根节点
                doc.add(element);
                writer.write(doc);
            }

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void CreateOrdersXml(int carid,int days,int type) throws Exception {

        CarBase car=DBUtils.GetCarById(carid);

        SAXReader reader = new SAXReader();
        Document read = reader.read("orders.xml");
        Element root = read.getRootElement();

        //创建一个Order节点
        Element newOrder = DocumentHelper.createElement("order");
        Attribute id = DocumentHelper.createAttribute(newOrder, "id", String.valueOf(carid));
        newOrder.add(id);
        root.add(newOrder);

        //创建一个name 节点
        Element name = DocumentHelper.createElement("name");
        name.setText(car.getName());
        newOrder.add(name);

        Element rentDays = DocumentHelper.createElement("租车时间");
        rentDays.setText(String.valueOf(days));
        newOrder.add(rentDays);

        Element sunPrice = DocumentHelper.createElement("sunPrice");
        sunPrice.setText(String.valueOf(days*car.getPrice()));
        newOrder.add(sunPrice);

        if(type==1){  //货车
            Element csize = DocumentHelper.createElement("载重量");
            csize.setText(car.getcSize());
            newOrder.add(csize);
        }
        if(type==2){ //客车
            Element psize = DocumentHelper.createElement("载客量");
            psize.setText(car.getpSize());
            newOrder.add(psize);
        }
        if(type==3){ //皮卡
            Element csize = DocumentHelper.createElement("载重量");
            csize.setText(car.getcSize());
            newOrder.add(csize);

            Element psize = DocumentHelper.createElement("载客量");
            psize.setText(car.getpSize());
            newOrder.add(psize);
        }

        try {
            //回写操作
            OutputFormat format = OutputFormat.createPrettyPrint();  //创建一个xml书写模板
            XMLWriter xmlWriter = new XMLWriter(new FileOutputStream("result.xml"), format);
            xmlWriter.write(read);  //将内存中的domcument对象doc写入到xml文件中
            xmlWriter.close();

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("回写失败！");
        }

    }

}
